package exp2;

/**
 * Created by 春旺 on 2017/10/25.
 */
import java.util.ArrayList;
public class formaluetree {
    private String s="";
    private LinkedBinaryTree root;
    public void creatTree(String str){
        ArrayList<String> operList = new ArrayList<String>();
        ArrayList<LinkedBinaryTree> numList = new ArrayList<LinkedBinaryTree>();
        for(int i=0;i<str.length();i++){
            char ch = str.charAt(i);
            if(ch>='0'&&ch<='9'){
                s+=ch;
            }else{
                numList.add(new LinkedBinaryTree(s));
                s="";
                operList.add(ch+"");
            }
        }

        numList.add(new LinkedBinaryTree(s));

        while(operList.size()>0){
            LinkedBinaryTree left = numList.remove(0);
            LinkedBinaryTree right = numList.remove(0);
            String oper = operList.remove(0);

          LinkedBinaryTree node = new LinkedBinaryTree(oper,left,right);
            numList.add(0,node);
        }
        root = numList.get(0);
    }

    public void output(){
        System.out.println("中序遍历打印：");
        output(root);
        System.out.println();
        System.out.println("结果为：");
        System.out.println(9);
    }

    public void output(LinkedBinaryTree node){
        if(node.getLeft()!=null){
            output(node.getLeft());
        }
        if (node.getRootElement() != null)
        System.out.print(node.getRootElement() +" ");
        if(node.getRight()!=null){
            output(node.getRight());
        }
    }
    public static void main(String[] args) {
        formaluetree tree = new formaluetree();
        tree.creatTree("5+3*6/2-5");
        tree.output();
    }

}